package Unicart.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.*;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class UserProfilePage extends AnyPage {

  public UserProfilePage(PageManager pages) {
		super(pages);
  }
	
  @FindBy(id = "account_name")
  private WebElement usernameField;
  
  @FindBy(id = "account_surname")
  private WebElement usersurnameField;
	
  @FindBy(id = "account_email")
  private WebElement emailField;
  
  @FindBy(id = "account_login")
  private WebElement loginField;
  
  @FindBy(id = "account_password_password")
  private WebElement passwordField;
  
  @FindBy(id = "account_password_password_repeat")
  private WebElement password2Field;
  
  @FindBy(name = "submit")
  private WebElement saveButton;
  
  public String getUsername() {
    return usernameField.getAttribute("value");
  }
  
  public String getUsersurname() {
	return usersurnameField.getAttribute("value");
  }

  public String getEmail() {
    return emailField.getAttribute("value");
  }
  
  public String getLogin() {
	return loginField.getAttribute("value");
  }

  public UserProfilePage setUsernameField(String text) {
	usernameField.click();
	usernameField.clear();
	usernameField.sendKeys(Keys.HOME);
	usernameField.sendKeys(text);
	return this;
  }
  
  public UserProfilePage setUserSurnameField(String text) {
	usersurnameField.click();
	usersurnameField.clear();
	usersurnameField.sendKeys(Keys.HOME);
	usersurnameField.sendKeys(text);
	return this;
  }
  
  public UserProfilePage setEmailField(String text) {
    emailField.sendKeys(text);
    return this;
  }

  public UserProfilePage setPassword(String text) {
    passwordField.sendKeys(text);
    return this;
  }

  public UserProfilePage setPassword2(String text) {
    password2Field.sendKeys(text);
    return this;
  }
  
  public UserProfilePage clickSaveButton() {
	saveButton.click();
	return this;
  }

  public UserProfilePage ensurePageLoaded() {
    super.ensurePageLoaded();
    wait.until(presenceOfElementLocated(By.cssSelector("input#account_name")));
    return this;
  }
  
  public UserProfilePage ensureChangeUserInfo() {
	 super.ensurePageLoaded();
	 wait.until(presenceOfElementLocated(By.className("flash-notice")));
	 return this;
  }
}
